﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using Microsoft.SharePoint.Utilities;
using UlsLogViewer.Entities;

namespace UlsLogViewer.Helpers
{
    public class UlsLogCollector
    {
        public static IEnumerable<LogFile> GetUlsLogFiles()
        {
            var defaultUlsLogLocation = SPUtility.GetGenericSetupPath("LOGS");
            var logFiles = GetUlsLogFile(defaultUlsLogLocation).ToList();
            return logFiles;
        }

        private static IEnumerable<LogFile> GetUlsLogFile(string ulsLogLocation)
        {
            var ulsdir = new DirectoryInfo(ulsLogLocation);
            if (ulsdir.Exists)
            {
                return ulsdir.GetFiles("*.log").Where(x => !x.FullName.StartsWith("PSCDiagnostics")).Select(y => new LogFile(y));
            }
            return new List<LogFile>();
        }
    }
}
